package learnjava.jvm;

public class TestSyn {

    static Integer mutex = 0;

    public static void main(String[] args) {

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(()->{
                    while (!mutex.equals(0)) {
                        try {
                            mutex.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println("线程0运行了");
                    synchronized (mutex) {//抢占信号量
                        mutex++;
                        mutex.notifyAll();
                    }
        }).start();

        new Thread(()->{
            while (!mutex.equals(1)) {
                try {
                    mutex.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程1运行了");
            synchronized (mutex) {//抢占信号量
                mutex++;
                mutex.notifyAll();
            }
        }).start();

        new Thread(()->{
            while (!mutex.equals(2)) {
                try {
                    mutex.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程2运行了");
            synchronized (mutex) {//抢占信号量
                mutex++;
                mutex.notifyAll();
            }
        }).start();

        new Thread(()->{
            while (!mutex.equals(3)) {
                try {
                    mutex.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("线程3运行了");
            synchronized (mutex) {//抢占信号量
                mutex++;
                mutex.notifyAll();
            }
        }).start();

    }

}
